<template>
  <div class="xn-tabs">
    <el-tabs v-model="activedSelf" :type="type" class="inner-tabs" @tab-click="changeTabType">
      <el-tab-pane v-for="item in tabs" :key="item.key" :name="item.key">
        <template slot="label">
          <slot :item="item">
            <span>{{ item.label }}</span>
          </slot>
        </template>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  name: 'XnTab',
  model: {
    prop: 'actived',
    event: 'valueChanged',
  },
  props: {
    tabs: {
      type: Array,
    },
    actived: {
      type: String,
    },
    type: {
      type: String,
      default: 'border-card',
    },
  },
  data() {
    return {
      activedSelf: '',
    }
  },
  watch: {
    actived(val) {
      this.activedSelf = val
    },
    activedSelf(val) {
      this.$emit('valueChanged', val)
    },
  },
  mounted() {
    if (this.tabs && this.tabs.length) {
      this.activedSelf = this.actived || this.tabs[0].key
    }
  },
  methods: {
    changeTabType(tabItem) {
      this.$nextTick(() => {
        this.$emit('change', tabItem.name)
        this.$emit('index-change', tabItem.index)
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.xn-tabs {
  ::v-deep .inner-tabs {
    height: 100%;
    box-shadow: none;
    border: none;
    .el-tabs__content {
      display: none;
    }
    .el-tabs__item {
      font-size: 16px;
      color: #666;
      &.is-active {
        color: #333;
        font-weight: 600;
      }
    }
    .el-tabs__nav-prev {
      top: -3px;
      left: 4px;
    }
    .el-tabs__nav-next {
      top: -3px;
      right: 4px;
    }
  }
}
</style>
